package com.example.user.myhjjf_app.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.view.Activity_Pzlist;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by User on 2017/8/29.
 */

public class Implementation_operation implements SiteOperation{
    private Context context;
    String vale;
    public Implementation_operation(Context context){
        this.context = context;
    }
    @Override
    public void Add(HashMap map, String url) {
        OkGo.<String>post(url)
                .tag(this)
                .params(map)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                             vale = jsonObject.getString("value");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Toast.makeText(context, vale, Toast.LENGTH_SHORT).show();
                        ((Activity)context).setResult(1);
                        ((Activity)context).finish();
                    }
                });
    }

    @Override
    public void delete(final String url, final String id) {
        new AlertDialog.Builder(context).setTitle("系统提示")
                .setMessage("您确定要删除吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        OkGo.<String>post(url)
                                .tag(this)
                                .params("id",id)
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(Response<String> response) {
                                        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                                        ((Activity)context).setResult(1);
                                        ((Activity)context).finish();
                                    }
                                });
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }
}
